package com.sskj.api.controller.admin;

import com.sskj.api.exception.LogicException;
import com.sskj.api.service.IFileUploadService;
import com.sskj.api.vo.UploadFileResult;
import com.sskj.common.util.ApiResponseUtil;
import com.sskj.common.util.FileUtil;
import com.sskj.common.vo.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "文件模块")
@RestController
@RequestMapping("/admin/file")
public class FileController {

    @Autowired
    private IFileUploadService fileUploadService;

    @Operation(summary = "图片上传")
    @PostMapping("uploadImage")
    public ApiResponse<UploadFileResult> uploadImage(@RequestParam("file") MultipartFile file) {
        if (file == null || file.isEmpty()) {
            throw new LogicException("上传文件不能为空");
        }

        if (!FileUtil.isImage(file.getOriginalFilename())) {
            throw new LogicException("只支持图片类型的文件");
        }

        if (file.getSize() > 10 * 1024 * 1024) {
            throw new LogicException("图片大小不能超过10M");
        }

        UploadFileResult upload = fileUploadService.upload(file);

        return ApiResponseUtil.success(upload);
    }
}
